<template>
  <el-dialog
    :title="ruleForm.id?'编辑客户':'新增客户'"
    :visible="showDialog"
    width="50%"
    @close="handleClose"
  >
    <el-form ref="ruleForm" :model="ruleForm" status-icon :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-form-item label="经纪" prop="agent">
        <el-select v-model="ruleForm.agent" placeholder="全部经纪">
          <el-option
            v-for="item in agentIdList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name" type="text" autocomplete="off" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="ruleForm.email" type="text" autocomplete="off" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="ruleForm.phone" type="text" autocomplete="off" />
      </el-form-item>
      <el-form-item label="微信" prop="wechat">
        <el-input v-model="ruleForm.wechat" type="text" autocomplete="off" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="ruleForm.desc" type="textarea" rows="3" autocomplete="off" />
      </el-form-item>
      <el-form-item label="图片" prop="image">
        <ImageUpload ref="userStaffPhoto" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="btnOk">确认</el-button>
      <el-button @click="handleClose">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import ImageUpload from '@/components/ImageUpload'
import { addClients, getCustomerById, editClients } from '@/api/customer'
export default {
  components: {
    ImageUpload
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    agentIdList: {
      type: Array,
      required: true

    }
  },
  data() {
    return {

      ruleForm: {
        agent: '',
        name: '',
        image: '',
        email: '',
        phone: '',
        wechat: '',
        desc: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入客户名', trigger: 'blur' },
          { min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请输入分类描述', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        wechat: [
          { required: true, message: '请输入微信', trigger: 'blur' }
        ],
        email: [
          { pattern: /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/, message: '输入正确的邮箱号', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async  btnOk() {
      try {
        await this.$refs.ruleForm.validate()
        const fileList = this.$refs.userStaffPhoto.fileList
        if (fileList[0] && fileList[0].status !== 'success') {
          this.$message.error('图片上传中，请稍等！')
          return
        }
        if (!fileList[0]) {
          this.$message.error('请上传图片')
          return
        }
        this.ruleForm.image = fileList[0].url
        if (this.ruleForm.id) {
        //  编辑
          await editClients(this.ruleForm)
          this.$emit('getCustomers')
        } else {
          // 添加
          await addClients(this.ruleForm)
          this.$emit('getCustomers', 'add')
        }

        this.$message.success('操作成功')
        this.handleClose()
      } catch (error) {
        console.log(error)
      }
    },
    async getCustomerById(id) {
      const { data } = await getCustomerById(id)
      this.ruleForm = data
      this.$refs.userStaffPhoto.fileList.push({ url: data.image })
    },
    handleClose() {
      this.$refs.ruleForm.resetFields()
      this.ruleForm = {
        agent: '',
        name: '',
        image: '',
        email: '',
        phone: '',
        wechat: '',
        desc: ''
      }
      this.$refs.userStaffPhoto.fileList = []
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>

</style>
